import { Injectable } from '@nestjs/common';
import { PrismaService } from '@/common/service/prisma/prisma.service';
import { Response } from 'express';
import { exportTable } from '@/common/utils';
import { QueryFrontAccountDto, CreateFrontAccountDto, UpdateFrontAccountDto } from '../dto/index';
import { Prisma } from '@prismaClient';
import { isNotEmpty,isEmpty } from 'class-validator';
@Injectable()
export class FrontAccountService {
  constructor(private prisma: PrismaService) { }
  /**@description 查询front_account所有 */
  async selectAccountAll () {
    return this.prisma.frontAccount.findMany()
  }
  /**@description 分页查询front_account列表 */
  async selectAccountList (q: QueryFrontAccountDto) {
    let queryCondition: Prisma.FrontAccountWhereInput = {}
        if (isNotEmpty(q["id"])) {
            queryCondition.id = {
              equals: q.id
            }
        }
        if (isNotEmpty(q["userName"])) {
            queryCondition.userName = {
              contains: q.userName
            }
        }
    return {
      rows: await this.prisma.frontAccount.findMany({
        skip: (q.pageNum - 1) * q.pageSize,
        take: q.pageSize,
        where: queryCondition,
      }),
      total: await this.prisma.frontAccount.count({
        where: queryCondition
      })
    }
  }
  /**@description 查询front_account详情 */
  async selectAccountById (id: number) {
    return this.prisma.frontAccount.findUnique({
      where: {
        id
      }
    })
  }
  /**@description 新增front_account */
  async addAccount (frontAccount: CreateFrontAccountDto) {
    if(isEmpty(frontAccount["userName"])) {
      delete frontAccount["userName"]
    } 
    if(isEmpty(frontAccount["password"])) {
      delete frontAccount["password"]
    } 
    if(isEmpty(frontAccount["nickName"])) {
      delete frontAccount["nickName"]
    } 
    return await this.prisma.frontAccount.create({
      data: frontAccount
    })
  }
  /**@description 修改front_account */
  async updateAccount (frontAccount: UpdateFrontAccountDto) {
    if(isEmpty(frontAccount["userName"])) {
      delete frontAccount["userName"]
    } 
    if(isEmpty(frontAccount["password"])) {
      delete frontAccount["password"]
    } 
    if(isEmpty(frontAccount["nickName"])) {
      delete frontAccount["nickName"]
    } 
    return await this.prisma.frontAccount.update({
        where: {
          id: frontAccount.id
        },
        data: frontAccount
      })
  }
  /**@description 批量删除front_account */
  async deleteAccountByIds (ids: number[]) {
    return this.prisma.frontAccount.deleteMany({
      where: {
        id: {
          in: ids
        }
      }
    })
  }
  /**@description 单个删除front_account */
  async deleteAccountById (id: number) {
    return this.prisma.frontAccount.delete({
      where: {
        id
      }
    })
  }
  /**@description 导出front_account所有数据为xlsx */
  async exportAccount (res: Response) {
    let title = ["id","user_name","password","nick_name"]
    let data = (await this.prisma.frontAccount.findMany()).map(v => Object.values(v))
    data.unshift(title)
    exportTable(data, res)
  }
}